package cn.biplam.front.component.view.systemManage.sysprivi.privilege.deploy.del;

import cn.biplam.common.ajxcode.SystemCode.PriviDeployCode;
import cn.biplam.common.bean.md.IndexTable;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.PriviDeployOptCode;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.struc.SysStruc.PriviDeployStruc;
import cn.biplam.common.struc.SysStruc.PriviStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.view.systemManage.sysprivi.privilege.PriviDeployManage;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.template.base.Text;
import cn.biplam.front.template.control.Confirm;

/**
 *  删除系统权限大类
 * @author XiaoRuxing Dec 7, 20093:56:36 PM
 * @version 1.0
 */
public class PriviDeployDel extends ComponentBase{

	public PriviDeployDel(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 * @return 删除提示界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow delPanel() throws ErrorMessage{
		HtmlWindow div;
		//1.用Deploy_ID查询发布权限大类的主题ID和名称
		OperatorResult rs = Operator.get(PriviDeployOptCode.Qurey_By_ID, 
				this.mUser.getUserID(), 
				new DataFilter(this.recordId));
		if(!TOOLS.exist(rs)){
			throw new ErrorMessage("Cann't query deployed Theme by deploy_id:("+this.recordId+")");
		}
		String rootId=rs.getValue(PriviDeployStruc.ROOT_ID);
		String priviTypeName=rs.getValue(PriviDeployStruc.NAME);

		//3.判断权限大类是否有下级
		IndexTable boBean = IndexTable.getNewInstance(mUser.getUserID(),
				PriviStruc.Table_Name,
				rootId);

		if(boBean.getIsLeaf()){//已发布权限大类叶子节点
			/**创建删除提示*/
			StringBuilder sb = new StringBuilder();
			sb.append(CaptionComp.get(mUser, SysCaption.Confirm));
			sb.append(CaptionComp.get(mUser, SysCaption.Delete));
			sb.append(" ");
			sb.append(priviTypeName);
			sb.append(" ?");
			
			/**生成提示信息窗口*/
			Confirm delete = new Confirm(sb.toString(),recordId);
			delete.bind(PriviDeployCode.Privi_Deploy_Del_Submit);
			div=delete.get();
			
		}else{//已发布有下级
			StringBuilder sb = new StringBuilder();
			sb.append(CaptionComp.get(mUser, SysCaption.Delete));
			sb.append(" ");
			sb.append(priviTypeName);
			sb.append(" ");
			sb.append(CaptionComp.get(mUser, SysCaption.Cancel));
			Confirm delete = new Confirm(sb.toString(),recordId);
			HtmlWindow confirm=new Text(CaptionComp.get(mUser, SysCaption.CantDeleteTreeNode)).get();
			confirm.setFontColor("red");
			delete.put(confirm);
			div=delete.get();
			div.bind(SysName.PromptSubmitID,Window_Event.OnClick,PopupScript.cancel(SysName.PromptID));
		}
		return div;
	}
	
	/**
	 *  删除权限大类提交
	 * @return 刷新权限大类列表脚本
	 * @throws ErrorMessage 
	 */
	public HtmlWindow delSubmit() throws ErrorMessage{
		//删除权限大类数据提交
		DataFilter filter = new DataFilter(recordId);
		Operator.get(PriviDeployOptCode.Del, mUser.getUserID(), filter);
		HtmlWindow win = new HtmlDiv();
		win.bind(AjxScript.getA(PriviDeployManage.Privi_Deploy_Main, //刷新容器ID
				PriviDeployManage.Privi_Deploy_Main,					 //刷新动作ID
				"del"
				));
		return win;
	}
}
